﻿using Dynamics.NAV.Metro.Common;
using Dynamics.NAV.Metro.Data;
using System;
using System.Collections.Generic;
using System.Composition;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The User Control item template is documented at http://go.microsoft.com/fwlink/?LinkId=234236

namespace Dynamics.NAV.Metro.Controls.Settings
{
    public sealed partial class ConnectionSettingsControl : UserControl
    {
        public ConnectionSettingsControl()
        {
            this.InitializeComponent();
            
            txtWebUrl.Text = SettingsStorage.GetLocalSetting<string>(App.DEFAULT_URL_KEY);
            txtUsername.Text = SettingsStorage.GetLocalSetting<string>(App.DEFAULT_USERNAME_KEY);
            txtPassword.Password = SettingsStorage.GetLocalSetting<string>(App.DEFAULT_PASSWORD_KEY);
            txtMaxRecords.Text = SettingsStorage.GetLocalSetting<int>(App.DEFAULT_MAX_REC_KEY).ToString();
            txtRecPerPage.Text = SettingsStorage.GetLocalSetting<int>(App.DEFAULT_REC_PER_PAGE_KEY).ToString();
        }

        private void txtWebUrl_TextChanged(object sender, TextChangedEventArgs e)
        {
            SettingsStorage.SetLocalSetting(App.DEFAULT_URL_KEY, txtWebUrl.Text);
        }

        private void txtUsername_TextChanged(object sender, TextChangedEventArgs e)
        {
            SettingsStorage.SetLocalSetting(App.DEFAULT_USERNAME_KEY, txtUsername.Text);
        }

        private void txtPassword_PasswordChanged(object sender, RoutedEventArgs e)
        {
            SettingsStorage.SetLocalSetting(App.DEFAULT_PASSWORD_KEY, txtPassword.Password);
        }

        private void txtMaxRecords_TextChanged(object sender, TextChangedEventArgs e)
        {
            int tmpInt;
            if (int.TryParse(txtMaxRecords.Text, out tmpInt))
                SettingsStorage.SetLocalSetting(App.DEFAULT_MAX_REC_KEY, tmpInt);
        }

        private void txtRecPerPage_TextChanged(object sender, TextChangedEventArgs e)
        {
            int tmpInt;
            if (int.TryParse(txtRecPerPage.Text, out tmpInt))
                SettingsStorage.SetLocalSetting(App.DEFAULT_REC_PER_PAGE_KEY, tmpInt);
        }
    }
}
